<?php

// Include Claroline Kernel
require dirname(__FILE__) . '/../../claroline/inc/claro_init_global.inc.php';

// Include libraries 
require get_path('includePath') . '/lib/embed.lib.php';
require get_path('includePath') . '/lib/sendmail.lib.php';
require get_path('includePath') . '/lib/datavalidator.lib.php';

// Include configuration
include_once claro_get_conf_repository().'CONTACT.conf.php';

// Initialise variables
$error = false ;
$dialogBox = '';
$display = 'display_form' ;

// Page & form parameters
$cmd = isset($_REQUEST['cmd'])?$_REQUEST['cmd']:null;

if ( claro_is_user_authenticated() )
{
    // Get user name and email
    $form_data['name'] = claro_get_current_user_data('firstName') . ' ' . claro_get_current_user_data('lastName');
    $form_data['email'] = claro_get_current_user_data('mail');
}
else
{
    $form_data['name'] = isset($_REQUEST['name'])?$_REQUEST['name']:null;
    $form_data['email'] = isset($_REQUEST['email'])?$_REQUEST['email']:null;
}

$form_data['subject'] = isset($_REQUEST['subject'])?$_REQUEST['subject']:null;
$form_data['message']= isset($_REQUEST['message'])?$_REQUEST['message']:null;
    
$form_required_fields = get_conf('applet_contact_form_required_fields');

if ( empty($form_required_fields) || ! is_array($form_required_fields) ) 
{
    $form_required_fields = array();
}

// Main section

if ( $cmd == 'rqSend' )
{
    /**
     * Form validation
     */

    $validator = new DataValidator();

    $dataList = array('name' => $form_data['name'] ,
                      'email' => $form_data['email'] ,
                      'subject' => $form_data['subject'] ,
                      'message' => $form_data['message'] );

    $validator->setDataList($dataList);

    if ( in_array('name', $form_required_fields) )
    {
        $validator->addRule('name',get_lang('Name is missing'),'required');
    }

    if ( in_array('email',$form_required_fields) )
    {
        $validator->addRule('email',get_lang('Email is missing'),'required');
    }

    $validator->addRule('email', get_lang('The email address is not valid'), 'email');

    if ( in_array('subject',$form_required_fields) )
    {
        $validator->addRule('subject',get_lang('Subject is missing'),'required');
    }

    if ( in_array('message',$form_required_fields) )
    {
        $validator->addRule('message',get_lang('Message is missing'),'required');
    }

    if ( $validator->validate(DATAVALIDATOR_STRICT_MODE) )
    {
        /**
         * Send mail
         */
        
        $email_list = array();
        
        if ( get_conf('applet_contact_email_list') != null )
        {
            $email_list = get_conf('applet_contact_email_list');
        }
        else
        {
            $email_list[] = get_conf('administrator_email');
        }

        $subject = $form_data['subject'];
        $message = $form_data['message'];
        $from = $form_data['email'];
        $fromName = $form_data['name'];

        foreach ( $email_list as $to )
        {
            var_dump($subject, $message, $to, '', $from, $fromName);
            claro_mail($subject, $message, $to, '', $from, $fromName) ;
        }

        $dialogBox = get_lang('Message successfully sent');

        $display = 'display_confirmation';    
    }
    else
    {
        foreach ( $validator->getErrorList() as $thisError )
		{
		    $dialogBox .= $thisError. '<br />' ;
		}        
        $error = true ;
    }
}

// Display section
$claroline = new ClarolineScriptEmbed();

$html = '<h1>' . get_conf('applet_contact_page_title') . '</h1>';

switch ( $display )
{

    case 'display_confirmation' :
            
        $html .= claro_html_message_box($dialogBox);
    
        break ;

    case 'display_form' :

        if ( $error )
        {
            $html .= claro_html_message_box($dialogBox);
        }

        if ( get_conf('applet_contact_page_html_header') != '' )
        {
            $html .= '<p>' . get_conf('applet_contact_page_html_header') . '</p>' . "\n" ;
        }

        $html .= '<form action="'.$_SERVER['PHP_SELF'].'" method="post">' . "\n"
            . form_input_hidden('cmd', 'rqSend')
            . form_input_hidden('claroFormId', uniqid('') );

        $html .= '<table class="claroRecord" cellpadding="3" cellspacing="0" border="0">' . "\n";

        if ( ! claro_is_user_authenticated() )
        {
            // Name
            $html .= form_input_text('name', $form_data['name'], get_lang('Name'), (in_array('name', $form_required_fields)?true:false) );
    
            // Email
            $html .= form_input_text('email', $form_data['email'], get_lang('Email'), (in_array('email', $form_required_fields)?true:false) );
        }

        // Subject
        $html .= form_input_text('subject', $form_data['subject'], get_lang('Subject'), (in_array('subject', $form_required_fields)?true:false) );

        // Message
        $html .= form_input_textarea('message', $form_data['message'], get_lang('Message'), (in_array('message', $form_required_fields)?true:false) );

        // Submit
        $html .= form_row( get_lang('Send') . '&nbsp;: ',
        '<input type="submit" value="' . get_lang('Ok') . '" />&nbsp;'
        . claro_html_button('contact.php', get_lang('Cancel')) );

        $html .= form_row('&nbsp;', '<small>' . get_lang('<span class="required">*</span> denotes required field') . '</small>');

        $html .= '</table>' . "\n"
        .        '</form>' . "\n"
        ;        

        /**
         * TODO : applet_contact_form_message_type_list
         */        

        if ( get_conf('applet_contact_page_html_footer') != '' )
        {
            $html .= '<p>' . get_conf('applet_contact_page_html_footer') . '</p>' . "\n" ;
        }

        break ;
}

// Script output
$claroline->setContent($html);
$claroline->output();

?>
